//本地调试模拟jsbridge
(function() {
  window.AppUtil.trigger("YzJSBridgeReady");

  function call(apiName, data, callback) {
    if (data.apiName == "scan" || apiName == "scan") {
      scan(callback);
    } else if (data.apiName == "getNearbyWIFI" || apiName == "getNearbyWIFI") {
      getNearbyWIFI(callback);
    } else if (data.apiName == "startBroadcast" || apiName == "startBroadcast") {
      startBroadcast(callback);
    } else if (data.apiName == "stopBroadcast" || apiName == "stopBroadcast") {
      stopBroadcast(callback);
    } else if (data.apiName == "selectPicture" || apiName == "selectPicture") {
      selectPicture(callback);
    } else if (data.apiName == "takePicture" || apiName == "takePicture") {
      takePicture(callback);
    
    } else if (data.apiName == "uploadPicture" || apiName == "uploadPicture") {
      uploadPicture(callback);
    }
  }

  //扫码
  function scan(callback) {
    let result = { success: true, resultObject: null, msg: "" };
    let qrCode = decodeURIComponent(window.location.hash).split("scanQr=");
    if (!qrCode[1]) {
      result.msg = "将地址栏中加入scanQr参数，参数值为模拟扫到的二维码内容";
    } else {
      result.resultObject = qrCode[1];
    }
    callback && callback(JSON.stringify(result));
  }
  //获取附近WiFi
  function getNearbyWIFI(callback) {
    let result = { success: true, resultObject: ["YunZong", "YunZong2"], msg: "",code:0 };
    callback && callback(JSON.stringify(result));
  }
  //开启广播
  function startBroadcast(callback) {
    let result = { success: true, resultObject: null, msg: "开启广播成功",code:0 };
    callback && callback(JSON.stringify(result));
  }
  //关闭广播
  function stopBroadcast(callback) {
    let result = { success: true, resultObject: null, msg: "关闭广播成功",code:0 };
    callback && callback(JSON.stringify(result));
  }
  //选择图片
  function selectPicture(callback) {
    let result = { success: true, resultObject: "/2019/02/28/17/948311551347510.jpg", msg: "选择图片成功",code:0 };
    callback && callback(JSON.stringify(result));
  }
  //拍照
  function takePicture(callback) {
    let result = { success: true, resultObject: "/2019/02/28/17/948311551347510.jpg", msg: "拍照成功",code:0 };
    callback && callback(JSON.stringify(result));
  }
  //上传图片
  function uploadPicture(callback) {
    let result = { success: true, resultObject: { success: true, resultObject: {imageUrl: "/2019/02/28/17/948311551347510.jpg"}, msg: "上传图片成功" ,code:0}, msg: "上传图片成功" ,code:0};
    const promise = new Promise(function(resolve, reject) {
      window.setTimeout(()=>{
        callback && callback(JSON.stringify(result));
        resolve();
      },1000);
    });
    
   
  }
  window.YzJSBridge = {};
  window.YzJSBridge.call = call;
})();
